package com.chb.system.controller;

import com.chb.system.annotation.OperLogAnnotation;
import com.chb.system.service.MenuService;
import com.chb.system.service.RoleMenuService;
import com.hos.entity.sys.Menu;
import com.hos.entity.sys.Role;
import com.hos.entity.sys.RoleMenu;
import com.hos.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: 陈海彬
 * @date: Created in 2023/12/7 19:38
 * @description:
 * @modified By:
 * @version:
 */
@RestController
@RequestMapping("system/menu")
public class MenuController {

    @Resource
    private MenuService menuService;
    @Resource
    private RoleMenuService roleMenuService;
    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 根据用户id查询菜单权限
     *
     * @param userId 用户id
     */
    @GetMapping("/findMenusByUserId/{userId}")
    public List<Menu> findMenusByUserId(@PathVariable Long userId) {
        return menuService.findMenusBuUserId(userId);
    }

    /**
     * 根据角色ID查询菜单信息
     *
     * @param roleId 角色id
     * @return
     */
    @GetMapping("/selectMenusByRoleId/{roleId}")
    public List<Menu> selectMenusByRoleId(@PathVariable Long roleId) {
        return menuService.selectMenusByRoleId(roleId);
    }

    // 保存用户和角色信息
    @PostMapping("/saveRoleMenu/{roleId}")
    @OperLogAnnotation(business_type = "系统业务-分配权限", operator_type = 1)
    public Result<Integer> saveRoleUser(@PathVariable Long roleId, @RequestBody List<Long> menuIds) {
        Integer integer = roleMenuService.saveRoleMenu(roleId, menuIds);
        // redisTemplate.delete("menu:leftMenu");
        return new Result<>(200,"分配成功",integer);
    }
}